package gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.*;
import javax.swing.border.EmptyBorder;


/**
 * @author Wray
 */
public class LoginGui {
	public LoginGui() {
		initLoginGui();
	}

	public void initLoginGui() {
		JFrame loginFrame = new JFrame("登录");

		loginFrame.setLayout(null); // 清除布局函数
		loginFrame.setResizable(false); // 设置窗体大小不可变
		loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		loginFrame.setLayout(new BorderLayout()); // 新建BorderLayout布局

//		// 背景
//		JPanel panel0 = new JPanel(new FlowLayout());
//		ImageIcon icon = new ImageIcon("image/logo.png");
//		JLabel label = new JLabel(icon);
//		panel0.add(label);
//		loginFrame.add(panel0, BorderLayout.NORTH);

		// 账号密码框
		JPanel userInfoPanel = new JPanel(new GridLayout(2, 1, 0, 20));
		userInfoPanel.setBorder(new EmptyBorder(10, 10, 10, 60));

		JLabel userNameLabel = new JLabel("账  号"); // 创建账号标签
		userNameLabel.setFont(new Font("微软雅黑", Font.BOLD, 16)); // 设置字体、样式、大小
		userNameLabel.setHorizontalAlignment(JTextField.CENTER); // 设置水平居中
		JTextField userNameTextField = new JTextField(10); // 创建文本框
		userNameTextField.setFont(new Font("微软雅黑", Font.PLAIN, 16)); // 设置字体、样式、大小

		JLabel pwdLabel = new JLabel("密  码"); // 创建密码标签
		pwdLabel.setFont(new Font("微软雅黑", Font.BOLD, 16)); // 设置字体、样式、大小
		pwdLabel.setHorizontalAlignment(JTextField.CENTER); // 设置居中
		JPasswordField pwdField = new JPasswordField(10); // 创建密码文本框
		pwdField.setEchoChar('*'); // 设置回显字符为 *
		pwdField.setFont(new Font("微软雅黑", Font.PLAIN, 16)); // 设置字体、样式、大小

		userInfoPanel.add(userNameLabel);
		userInfoPanel.add(userNameTextField);
		userInfoPanel.add(pwdLabel);
		userInfoPanel.add(pwdField);

		loginFrame.add(userInfoPanel, BorderLayout.CENTER);

		// 登录按钮
		JPanel loginPanel = new JPanel(new FlowLayout());

		JButton loginButton = new JButton("登录");
		// 登录按钮触发事件
		loginButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				loginButton.setText("正在登录......");
				String userName = userNameTextField.getText();
				String password = new String(pwdField.getPassword());
				if (userName.equals("123") &&password.equals("123")) {
					loginFrame.dispose();
					new MenuGui();
					//new ShoppingCartGui(Main.person.shoppingCart);
				} else {
					JOptionPane.showMessageDialog(loginFrame, "你输入的密码和账户名不匹配");
					pwdField.setText("");
					loginButton.setText("登录");
				}
			}
		});
		

		loginPanel.add(loginButton);

		loginFrame.add(loginPanel, BorderLayout.SOUTH);

		// Display the window.
		loginFrame.pack();
		loginFrame.setVisible(true);
		// 设置窗口居中显示
		SettingWindow.setFrameCenter(loginFrame);

	}

}
